import numpy as np import pandas as pd import matplotlib.pyplot as plt # Read the S&P 500 data df = pd.read_csv('/path/to/your/csv/file.csv') # Filtering the dataset to only include data from January 1, 1990, onwards df_1990 = df[df['Date'] >= '1990-01-01'].reset_index(drop=True) # Number of data points in the dataset starting from 1990 n_1990 = len(df_1990) # Calculate the daily returns for the actual S&P 500 data starting from 1990 s_actual_1990 = df_1990['Close'].values daily_returns_1990 = (s_actual_1990[1:] / s_actual_1990[:-1]) - 1 # Define the annual fee rate for 2x, 3x, and 4x leveraged products (1% annual fee) annual_fee_rate = 0.01 daily_fee_multiplier = 1 - (annual_fee_rate / 252) # Assuming 252 trading days in a year # Initialize arrays for 2x, 3x, and 4x leveraged products with daily rebalancing and fees starting from 1990 s_2x_leverage_daily_rebalance_fee_1990 = np.zeros(n_1990) s_3x_leverage_daily_rebalance_fee_1990 = np.zeros(n_1990) s_4x_leverage_daily_rebalance_fee_1990 = np.zeros(n_1990) s_2x_leverage_daily_rebalance_fee_1990[0] = s_actual_1990[0] s_3x_leverage_daily_rebalance_fee_1990[0] = s_actual_1990[0] s_4x_leverage_daily_rebalance_fee_1990[0] = s_actual_1990[0] # Perform the simulation with daily rebalancing and fees for t in range(1, n_1990): daily_return = (s_actual_1990[t] / s_actual_1990[t-1]) - 1 s_2x_leverage_daily_rebalance_fee_1990[t] = s_2x_leverage_daily_rebalance_fee_1990[t-1] * (1 + 2 * daily_return) * daily_fee_multiplier s_3x_leverage_daily_rebalance_fee_1990[t] = s_3x_leverage_daily_rebalance_fee_1990[t-1] * (1 + 3 * daily_return) * daily_fee_multiplier s_4x_leverage_daily_rebalance_fee_1990[t] = s_4x_leverage_daily_rebalance_fee_1990[t-1] * (1 + 4 * daily_return) * daily_fee_multiplier # Normalize the leveraged products and actual S&P 500 to start from 100 normalized_s_actual_1990 = s_actual_1990 / s_actual_1990[0] * 100 normalized_s_2x_leverage_daily_rebalance_fee_1990 = s_2x_leverage_daily_rebalance_fee_1990 / s_2x_leverage_daily_rebalance_fee_1990[0] * 100 normalized_s_3x_leverage_daily_rebalance_fee_1990 = s_3x_leverage_daily_rebalance_fee_1990 / s_3x_leverage_daily_rebalance_fee_1990[0] * 100 normalized_s_4x_leverage_daily_rebalance_fee_1990 = s_4x_leverage_daily_rebalance_fee_1990 / s_4x_leverage_daily_rebalance_fee_1990[0] * 100 # Plotting the graph plt.figure(figsize=(15, 6)) plt.plot(df_1990['Date'], normalized_s_actual_1990, label='Normalized Actual S&P 500', color='r') plt.plot(df_1990['Date'], normalized_s_2x_leverage_daily_rebalance_fee_1990, label='Normalized 2x Leveraged with Fee (Daily Rebalance)', color='g') plt.plot(df_1990['Date'], normalized_s_3x_leverage_daily_rebalance_fee_1990, label='Normalized 3x Leveraged with Fee (Daily Rebalance)', color='b') plt.plot(df_1990['Date'], normalized_s_4x_leverage_daily_rebalance_fee_1990, label='Normalized 4x Leveraged with Fee (Daily Rebalance)', color='m') plt.title('Normalized Actual S&P 500, 2x, 3x, and 4x Leveraged with Daily Rebalancing and Fees (Base Value = 100, Starting from 1990)') plt.xlabel('Date') plt.ylabel('Normalized Value') plt.legend() plt.grid(True) plt.show()